package com.chuantou.yuba.adapter;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.text.format.DateUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.chuantou.yuba.R;
import com.chuantou.yuba.api.to.FishingTO;
import com.chuantou.yuba.util.ActivityHelper;
import com.chuantou.yuba.util.BitmapBinder;
import com.chuantou.yuba.util.Constants;
import com.chuantou.yuba.util.DateUtil;
import com.chuantou.yuba.util.ViewHolder;

public class FishingAdapter extends BaseAdapter {
	private Activity mActivity;
	private List<FishingTO> mList = new ArrayList<FishingTO>();

	public FishingAdapter(Activity activity) {
		this.mActivity = activity;
	}

	public void add(FishingTO to) {
		this.mList.add(to);
	}

	public void add(List<FishingTO> list) {
		if (list != null && !list.isEmpty()) {
			this.mList.addAll(list);
		}
	}

	public void clear() {
		this.mList.clear();
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = View.inflate(mActivity, R.layout.fishing_item, null);
		}
		FishingTO fishingTO = mList.get(position);
		NetworkImageView avatarView = ViewHolder.get(convertView, R.id.avatar);
		BitmapBinder.bindCircularNetImage(mActivity, avatarView, fishingTO.getAvatar(), R.drawable.default_avatar_round_small);
		avatarView.setTag(fishingTO);
		avatarView.setOnClickListener(mProfileClickListener);
		TextView sexView = ViewHolder.get(convertView, R.id.sex);

		int sex = fishingTO.getSex();
		if (sex == Constants.SEX_FEMALE) {
			sexView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.sex_female, 0, 0, 0);
			sexView.setTextColor(mActivity.getResources().getColor(R.color.sex_color_female));
		} else {
			sexView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.sex_male, 0, 0, 0);
			sexView.setTextColor(mActivity.getResources().getColor(R.color.sex_color_male));
		}
		Date date = DateUtil.getDateObj(fishingTO.getBirthday(), DateUtil.PATTERN_YMD);
		int age = (int) ((System.currentTimeMillis() - date.getTime()) / (365L * 24 * 60 * 60 * 1000));
		sexView.setText(String.valueOf(age));
		TextView nicenameView = ViewHolder.get(convertView, R.id.nickname);
		nicenameView.setText(fishingTO.getUsername());
		nicenameView.setTag(fishingTO);
		nicenameView.setOnClickListener(mProfileClickListener);
		TextView timeView = ViewHolder.get(convertView, R.id.time);
		String time = DateUtils.formatDateTime(mActivity, fishingTO.getPublishTime() * 1000, DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE
				| DateUtils.FORMAT_ABBREV_ALL);
		timeView.setText(time);
		TextView contentView = ViewHolder.get(convertView, R.id.content);
		contentView.setText(fishingTO.getContent());
		TextView fishingTimeView = ViewHolder.get(convertView, R.id.fishing_time);
		Date startDate = new Date(fishingTO.getStartTime() * 1000);
		Date endDate = new Date(fishingTO.getEndTime() * 1000);
		String fishingTime = null;
		if (DateUtil.isSameDay(startDate, endDate)) {
			String startTime = DateUtil.getDateString(startDate.getTime(), DateUtil.PATTERN_MDHM);
			String endTime = DateUtil.getDateString(endDate.getTime(), DateUtil.PATTERN_HN);
			fishingTime = startTime + "~" + endTime;
		} else {
			String startTime = DateUtil.getDateString(startDate.getTime(), DateUtil.PATTERN_MDHM);
			String endTime = DateUtil.getDateString(endDate.getTime(), DateUtil.PATTERN_MDHM);
			fishingTime = startTime + "~" + endTime;
		}
		fishingTimeView.setText(fishingTime);

		TextView fishingCostView = ViewHolder.get(convertView, R.id.fishing_cost);
		int cost = fishingTO.getCost();
		if (cost > 0) {
			fishingCostView.setText(mActivity.getString(R.string.fishing_cost_format, cost));
		} else {
			fishingCostView.setText(R.string.free);
		}
		TextView fishingLocationView = ViewHolder.get(convertView, R.id.fishing_location);
		fishingLocationView.setText(fishingTO.getAddress());
		TextView fishingPeopleView = ViewHolder.get(convertView, R.id.fishing_people);
		int max = fishingTO.getMaxPeople();
		if (max > 0) {
			fishingPeopleView.setText(mActivity.getString(R.string.fishing_people_format, fishingTO.getJoinedCount(), max));
		} else {
			fishingPeopleView.setText(R.string.no_limit);
		}
		return convertView;
	}

	private OnClickListener mProfileClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Object tag = v.getTag();
			if (tag != null && tag instanceof FishingTO) {
				FishingTO fishingTO = (FishingTO) tag;
				ActivityHelper.toProfileActivity(mActivity, fishingTO.getUid());
			}
		}
	};
}